package View.list;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import javax.swing.JLabel;

import Management.Animation;

public class MyLabel extends JLabel implements MouseListener, FocusListener{
	private boolean isSelected = false;
	private boolean hasFocus = false;
	private boolean isEntered = false;
    private static final Color COLOR1 = new Color(153, 222, 253);
    private static final Color COLOR2 = new Color(213, 239, 252);
    private static final Color COLOR3 = new Color(241, 248, 253);
    
	public MyLabel(Animation anime) {
		super(anime.getName());
		setIcon(anime.getIcon());
//		setOpaque(false);
		addMouseListener(this);
		addFocusListener(this);
	}
	
    /**
     * Paints the value.  The background is filled based on selected.
     */
	public void paint(Graphics g) {
		if (isSelected) {
		    int W = getWidth();
		    int H = getHeight();
	        
		    GradientPaint GP = new GradientPaint(0, 0, COLOR3, 0, getHeight(), COLOR2, true);
		    Graphics2D g2d 	= (Graphics2D) g.create();

		    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
		    g2d.setPaint(GP);
		    g2d.fillRoundRect(0, 0, W - 1, H - 1, 10, 10);
		    g2d.setColor(COLOR1);
		   	g2d.drawRoundRect(0, 0, W - 1, H - 1, 10, 10);
		   	g2d.dispose();
		}
		else if (isEntered || hasFocus) {
		    int W = getWidth();
		    int H = getHeight();
	        
		    GradientPaint GP = new GradientPaint(0, 0, getBackground(), 0, getHeight(), COLOR2, true);
		    Graphics2D g2d 	= (Graphics2D) g.create();

		    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
		    g2d.setPaint(GP);
		    g2d.fillRoundRect(0, 0, W - 1, H - 1, 10, 10);
		    g2d.setColor(COLOR3);
		   	g2d.drawRoundRect(0, 0, W - 1, H - 1, 10, 10);
		   	g2d.dispose();
		}

		super.paint(g);
	}
	
	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		repaint();
	}

	public void setHasFocus(boolean hasFocus) {
		this.hasFocus = hasFocus;
		repaint();
	}

	public void setEntered(boolean isEntered) {
		this.isEntered = isEntered;
		repaint();
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		if (arg0.getClickCount() == 1) {
			setSelected(true);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		setEntered(true);
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		setEntered(false);
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		setHasFocus(true);
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		setHasFocus(false);
	}
}
